package com.xxm.audio

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.xxm.R
import com.xxm.databinding.ActivityMainBinding
import com.xxm.mediacodec.MediaCodecActivity

class MainActivity : AppCompatActivity() {
    private  val TAG = "MainActivity"
    private val PERMISSION_RECORD_AUDIO_REQUEST_CODE = 1;
    private val WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 2;
    private lateinit var binding: ActivityMainBinding

    private var adapter = AudioAdapter(mutableListOf())

    val model by lazy {
        ViewModelProvider(this)[AudioModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        checkPermission()

        binding.btnStop.setOnClickListener {
            model.release()
            model.stopRecord()
        }

        binding.btnStart.setOnClickListener {
            model.release()
            model.startRecord()
        }

        model.size.observe(this) {
            binding.tv.text = "大小:${it}"
        }

        binding.btnVideo.setOnClickListener {
            startActivity(Intent(this, MediaCodecActivity::class.java))
        }
        initView()
        model.getAudioFile()

//        Log.e(TAG,"FFmpege version:${FFmpegManager.getFFmpegVersion()}")
    }

    private fun checkPermission() {
        if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(
                arrayOf(Manifest.permission.RECORD_AUDIO),
                PERMISSION_RECORD_AUDIO_REQUEST_CODE
            )
        } else if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                WRITE_EXTERNAL_STORAGE_REQUEST_CODE
            )
        } else if (checkSelfPermission(Manifest.permission.READ_MEDIA_VIDEO) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                WRITE_EXTERNAL_STORAGE_REQUEST_CODE
            )
        } else if (checkSelfPermission(Manifest.permission.READ_MEDIA_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                WRITE_EXTERNAL_STORAGE_REQUEST_CODE
            )
        } else if (checkSelfPermission(Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                WRITE_EXTERNAL_STORAGE_REQUEST_CODE
            )
        }
    }

    private var curItem: AudioItem? = null

    private fun initView() {
        binding.rvFile.adapter = adapter
        binding.rvFile.layoutManager = LinearLayoutManager(this)
        adapter.setOnItemClickListener { adapter, view, position ->
            curItem?.let {
                it.isSelect = false
                adapter.notifyItemChanged(it.position)
            }
            curItem = adapter.getItem(position)
            curItem!!.position = position
            curItem!!.isSelect = true
            adapter.notifyItemChanged(position)
            binding.btnStartPlay.isEnabled = true
            model.release()
        }


        model.filePaths.observe(this) { list ->
            binding.btnStartPlay.isEnabled = false
            adapter.submitList(list)
        }

        binding.btnStartPlay.setOnClickListener {
            model.release()
            model.startPlay(curItem!!.name)
        }

        binding.btnStopPlay.setOnClickListener {
            model.release()
            model.stopPlay()
        }
        model.initBuffer()
    }

    private fun initAudio() {
    }

    override fun onDestroy() {
        super.onDestroy()
        model.stopRecord()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        checkPermission()
    }


}